package model

import (
	"quick-go/dao"
)

type User struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func DeleteUser(id int) error {
	err := dao.InitDB().Delete(&User{}, id).Error
	return err
}

func UpdateUser(id int, username string, age int) error {
	user := User{Name: username, Age: age}
	err := dao.InitDB().Model(&User{}).Where("id = ?", id).Updates(&user).Error
	return err
}

func CreateUser(username string, age int) (int, error) {
	user := User{Name: username, Age: age}
	err := dao.InitDB().Create(&user).Error
	return user.ID, err
}

func GetUserByID(id int) (User, error) {

	var user User
	err := dao.InitDB().Where("id = ?", id).First(&user).Error // First 查找一个，Find 查找多个
	return user, err
}

func GetUserLT3() ([]User, error) {
	var users []User
	err := dao.InitDB().Where("id < ?", 3).Find(&users).Error
	return users, err
}
